package com.osi.mes.qm.services;

import java.util.List;
import java.util.Vector;

import pnuts.lang.Context;

import com.datasweep.compatibility.client.ATHandler;
import com.datasweep.compatibility.client.ATRow;
import com.datasweep.compatibility.client.ATRowFilter;
import com.datasweep.compatibility.client.DatasweepException;
import com.datasweep.compatibility.client.DsList;
import com.osi.mes.base.BaseService;
import com.osi.mes.qm.base.constants.IATNames;
import com.osi.mes.qm.base.constants.IBasicConstants;
import com.osi.mes.qm.ftphandler.FTPFilesHandler;
import com.osi.mes.utils.StringUtils;

public class QmChkTbService extends BaseService {
	private String ftpHost;
	private int ftpPort;
	private String ftpUser;
	private String ftpPassword;

	public QmChkTbService(Context context) {
		super(context);
		initFTPConfig();
	}

	private void initFTPConfig() {
		DsList ftpList = getFunctions()
				.getList(IBasicConstants.FTP_LIST_CONFIG);
		if (ftpList == null)
			return;

		List<String> ftpConfigs = ftpList.getItems();
		if (ftpConfigs == null || ftpConfigs.size() == 0)
			return;

		ftpHost = ftpConfigs.get(0);
		ftpPort = new Integer(ftpConfigs.get(1)).intValue();
		ftpUser = ftpConfigs.get(2);
		ftpPassword = ftpConfigs.get(3);
	}

	public String getFtpHost() {
		return ftpHost;
	}

	public int getFtpPort() {
		return ftpPort;
	}

	public String getFtpUser() {
		return ftpUser;
	}

	public String getFtpPassword() {
		return ftpPassword;
	}

	// 通过检验报告单号，查询检验单
	public ATRow getChkTbByRptNo(String rptNo, int tbType)
			throws DatasweepException {
		String atDefName = IATNames.AT_CHK_TB;
		ATHandler atHandler = getFunctions().createATHandler(atDefName);
		ATRowFilter rowFilter = getFunctions().createATRowFilter(atDefName);
		rowFilter.forColumnNameEqualTo(IATNames.AT_CHK_TB_CHK_RPT_NO, rptNo);
		rowFilter.forColumnNameEqualTo(IATNames.AT_CHK_TB_TYPE, tbType);
		Vector matchedEqus = atHandler.getATRowsByFilter(rowFilter, false);

		if (matchedEqus == null || matchedEqus.size() == 0)
			return null;

		return (ATRow) matchedEqus.get(0);
	}

	// 上传文件
	public boolean uploadRptToFtp(String fileName, String ftpFolder)
			throws Exception {
		if (StringUtils.isNull(ftpHost))
			initFTPConfig();

		return FTPFilesHandler.uploadFile(ftpHost, ftpPort, ftpUser,
				ftpPassword, ftpFolder, fileName, true);
	}

	// 删除FTP上指定的文件
	public boolean deleteFTPFile(String fileName, String ftpFolder)
			throws Exception {
		if (StringUtils.isNull(ftpHost))
			initFTPConfig();

		if (StringUtils.isNull(fileName) || StringUtils.isNull(ftpFolder))
			return false;

		return FTPFilesHandler.deleteFile(ftpHost, ftpPort, ftpUser,
				ftpPassword, ftpFolder, fileName);
	}

	// 查看检验报告
	public void viewFTPFile(String ftpFoder) throws Exception {
		if (StringUtils.isNull(ftpHost))
			initFTPConfig();

		FTPFilesHandler.openFTPToSpecifiedDir(ftpHost, ftpUser, ftpPassword,
				ftpFoder);
	}

}
